package DBLayer;
import ModelLayer.Profile;
import java.sql.*;
import java.util.ArrayList;

public class DBProfile {
    // <editor-fold defaultstate="collapsed" desc="Attributes & Constructors">
    private Connection conn;

    public DBProfile() {
        conn = SQLConnector.getInstance().getConnection();
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="SQL SELECT Methods">
    public Profile getProfileByID(int id) {
        return selectSingle("SELECT `id_profile`, `name` FROM `ps_profile` LEFT JOIN `ps_profile_lang` USING (`id_profile`) WHERE (`id_lang` = " + Constants.DEFAULT_LANGUAGE + " OR `id_lang` IS NULL) AND `id_profile` = " + id + " LIMIT 0, 1;");
    }

    public ArrayList<Profile> getProfileList() {
        return selectMultiple("SELECT `id_profile`, `name` FROM `ps_profile` LEFT JOIN `ps_profile_lang` USING (`id_profile`) WHERE `id_lang` = " + Constants.DEFAULT_LANGUAGE + " OR `id_lang` IS NULL ORDER BY `id_profile` ASC;");
    }

    private Profile selectSingle(String sqlStatement) {
        Statement stmt = null;
        ResultSet result = null;
        Profile profile = new Profile();

        try {
            stmt = conn.createStatement();
            result = stmt.executeQuery(sqlStatement);

            if (result.next()) {
                profile = buildProfile(result);
            }
            System.out.println("Fetching Profile Object");
        } catch (SQLException e) {
            System.out.println("SQLException: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("VendorError: " + e.getErrorCode());
        } finally {
            if (result != null) {
                try {
                    result.close();
                } catch (SQLException e) {
                    System.out.println("SQLException: " + e.getMessage());
                    System.out.println("SQLState: " + e.getSQLState());
                    System.out.println("VendorError: " + e.getErrorCode());
                }
                result = null;
            }
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                    System.out.println("SQLException: " + e.getMessage());
                    System.out.println("SQLState: " + e.getSQLState());
                    System.out.println("VendorError: " + e.getErrorCode());
                }
                stmt = null;
            }
        }
        return profile;
    }

    private ArrayList<Profile> selectMultiple(String sqlStatement) {
        Statement stmt = null;
        ResultSet result = null;
        ArrayList<Profile> profiles = new ArrayList<Profile>();

        try {
            stmt = conn.createStatement();
            result = stmt.executeQuery(sqlStatement);

            while (result.next()) {
                Profile profile = new Profile();
                profile = buildProfile(result);
                profiles.add(profile);
            }
            System.out.println("Fetching Profile List");
        } catch (SQLException e) {
            System.out.println("SQLException: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("VendorError: " + e.getErrorCode());
        } finally {
            if (result != null) {
                try {
                    result.close();
                } catch (SQLException e) {
                    System.out.println("SQLException: " + e.getMessage());
                    System.out.println("SQLState: " + e.getSQLState());
                    System.out.println("VendorError: " + e.getErrorCode());
                }
                result = null;
            }
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                    System.out.println("SQLException: " + e.getMessage());
                    System.out.println("SQLState: " + e.getSQLState());
                    System.out.println("VendorError: " + e.getErrorCode());
                }
                stmt = null;
            }
        }
        return profiles;
    }

    private Profile buildProfile(ResultSet result) {
        Profile profile = new Profile();
        try {
            profile.setIDProfile(result.getInt("id_profile"));
            profile.setName(result.getString("name"));

        } catch (Exception e) {
            System.out.println("Exception: " + e.getMessage());
        }
        return profile;
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="SQL INSERT, UPDATE & DELETE Methods">
    public int addProfile(Profile profile) {
        int returnID = 0;
        Statement stmt = null;
        ResultSet result = null;

        String query = "INSERT INTO `ps_profile_lang` (`id_profile`, `id_lang`, `name`) "
                + "VALUES (" + profile.getIDProfile() + ", " + Constants.DEFAULT_LANGUAGE + ", '" + profile.getName() + "');";

        query += "INSERT INTO `ps_profile` (`id_profile`) "
                + "VALUES (" + profile.getIDProfile() + ");";
        try {
            stmt = conn.createStatement();
            stmt.executeUpdate(query, Statement.RETURN_GENERATED_KEYS);
            result = stmt.getGeneratedKeys();
            if (result.next()) {
                returnID = result.getInt(1);
            }
            System.out.println("Added Profile ID " + returnID);
        } catch (SQLException e) {
            System.out.println("SQLException: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("VendorError: " + e.getErrorCode());
        } finally {
            if (result != null) {
                try {
                    result.close();
                } catch (SQLException e) {
                    System.out.println("SQLException: " + e.getMessage());
                    System.out.println("SQLState: " + e.getSQLState());
                    System.out.println("VendorError: " + e.getErrorCode());
                }
                result = null;
            }
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                    System.out.println("SQLException: " + e.getMessage());
                    System.out.println("SQLState: " + e.getSQLState());
                    System.out.println("VendorError: " + e.getErrorCode());
                }
                stmt = null;
            }
        }
        return returnID;
    }

    public int updateProfile(Profile profile, int id) {
        int returnID = 0;
        Statement stmt = null;
	String query = "UPDATE `ps_profile` SET "
                + "`id_profile`   = "  + profile.getIDProfile() + ", "
                + "WHERE `id_profile` = " + id + " LIMIT 1; ";

        query += "UPDATE `ps_profile_lang` SET "
                + "`id_profile`   = "  + profile.getIDProfile() + ", "
                + "`name`         = "  + profile.getName() + " "
                + "WHERE `id_lang` = " + Constants.DEFAULT_LANGUAGE + " AND `id_profile` = " + id + " LIMIT 1;";

        try {
            stmt = conn.createStatement();
            stmt.executeUpdate(query);
            returnID = profile.getIDProfile();
            System.out.println("Updated Profile ID " + id);
        } catch (SQLException e) {
            System.out.println("SQLException: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("VendorError: " + e.getErrorCode());
        } finally {
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                    System.out.println("SQLException: " + e.getMessage());
                    System.out.println("SQLState: " + e.getSQLState());
                    System.out.println("VendorError: " + e.getErrorCode());
                }
                stmt = null;
            }
        }
        return returnID;
    }

    public void deleteProfile(int id) {
        Statement stmt = null;

        try {
            stmt = conn.createStatement();
            stmt.executeUpdate("DELETE FROM `ps_profile` WHERE `id_profile` = " + id + " LIMIT 1; DELETE FROM `ps_profile_lang` WHERE `id_lang` = " + Constants.DEFAULT_LANGUAGE + " AND `id_profile` = " + id + " LIMIT 1;");
            System.out.println("Deleted Profile ID " + id);
        } catch (SQLException e) {
            System.out.println("SQLException: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("VendorError: " + e.getErrorCode());
        } finally {
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                    System.out.println("SQLException: " + e.getMessage());
                    System.out.println("SQLState: " + e.getSQLState());
                    System.out.println("VendorError: " + e.getErrorCode());
                }
                stmt = null;
            }
        }
    }
    // </editor-fold>
}
